package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.Guru;
import com.baizhi.springboot.service.GuruService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GuruController {
    @Autowired
    private GuruService guruService;
    @RequestMapping("/daochuGuru")
    public void daochuGuru(HttpServletResponse response) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException {
        HSSFWorkbook workbook = guruService.daochuGuru();
        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode("guru.xls","utf-8"));
        workbook.write(response.getOutputStream());
    }
    @RequestMapping("/daoruGuru")
    @ResponseBody
    public Map daoruGuru(MultipartFile file) throws IOException {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            guruService.daoruGuru(file.getInputStream());
            map.put("isdaoru", "导入成功");
        }catch (Exception e) {
            e.printStackTrace();
            map.put("isdaoru", "导入失败");
        }
        return map;
    }
    @RequestMapping("/selectAllGuru")
    @ResponseBody
    public List<Guru> selectAllGuru() {
        return guruService.selectAllGuru();
    }
    @RequestMapping("/selectGuruByPage")
    @ResponseBody
    public Map selectGuruByPage(Integer page, Integer limit) {
        return guruService.selectGuruByPage(page,limit);
    }
    @RequestMapping("/uploadGuru")
    @ResponseBody
    public Map  uploadGuru(MultipartFile file) throws IOException {
        file.transferTo(new File("D:/uploadFile/"+file.getOriginalFilename()));
        String guruImage=file.getOriginalFilename();
        System.out.println(guruImage);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("guruImage",guruImage);
        return map;
    }
    @RequestMapping("/insertGuru")
    @ResponseBody
    public Map  insertGuru(Guru guru) throws IOException {
        HashMap<Object, Object> map = new HashMap<>();

        try {
            guruService.insertGuru(guru);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/deleteGuru")
    @ResponseBody
    public Map  deleteGuru(Integer id){
        HashMap<Object, Object> map = new HashMap<>();

        try {
            guruService.deleteGuru(id);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("/selectOneGuru")
    @ResponseBody
    public Map selectOneGuru(Integer id, HttpSession session) {
        Guru guru= guruService.selectOneGuru(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("guru", guru);
        return map;
    }

    @RequestMapping("/updateGuru")
    @ResponseBody
    public Map updateGuru(Guru guru) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            guruService.updateGuru(guru);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }
}
